// Source File Name:   ANYImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.MessageUtils;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.BN;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;
import com.sunwayhorizo.health.hl7.common.RimDataTypeException;


//import com.sunwayhorizo.health.ctb.hl7.common.RimDataTypeException;

// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            BNImpl, CSImpl, NullFlavorImpl

public abstract class ANYImpl implements ANY {

	public static final String RCS_ID = "$Header: ANYImpl.java 1.0 2006/09/16 10:39:31 mmoores noship $";
	private CS _nullFlavor;
	private boolean _isNull;
	private static final String CLASS_KEY = "CLASS";
	private static final String METHOD_KEY = "METHOD";
	private static final String PARAM_KEY = "PARAM";
	private static final String LITERAL_KEY = "LITERAL";
	private static final String MESSAGE_KEY = "MESSAGE";
	private static final String LOW_KEY = "LOW";
	private static final String HIGH_KEY = "HIGH";
	private static final String PRECISION_KEY = "PRECISION";
	private static final String PARSE_CODE = "CTB_CORE_GTS_PARSER";
	private static final String UNSUPPORTED_MESSAGE = "Operation &METHOD on data type &CLASS is not supported in this version of Healthcare.";
	private static final String PARSE_MESSAGE = "Invalid syntax encountered when parsing &CLASS literal: \"&LITERAL\"; &MESSAGE";
	private static final String NULL_MESSAGE = "Mandatory parameter &PARAM must be supplied for operation &METHOD on data type &CLASS";
	private static final String SYMBOL_MESSAGE = "The symbolic abbreviation \"&LITERAL\" is not supported in this version of Healthcare.";
	private static final String INTERVAL_MESSAGE = "Cannot construct interval with one boundary of \"&LOW\" and the other of \"&HIGH\". &CLASS must have matching boundary types.";
	private static final String HULLIVL_MESSAGE = "Abbreviated hull form requires that the high timestamp literal \"&HIGH\" has the same as or fewer characters than the low timestamp \"&LOW\".";
	private static final String PRECISION_MESSAGE = "&CLASS literal of length &PRECISION is not supported in this version of Healthcare.";
	private static final String TIMEZONE_MESSAGE = "Cannot compare TS \"&LOW\" with TS \"&HIGH\" because of incompatible timezone offsets (one of the timezones is missing).";
	private static final String DATE_MESSAGE = "Date string \"&LITERAL\" is not recognized by SimpleDateFormat";
	private static final String INVALID_BOOLEAN_MESSAGE = "Boolean literal \"&LITERAL\" is not recognized. Must be either \"true\" or \"false\".";
	private static final String RATIO_MESSAGE = "The denominator of a &CLASS may not be zero.";

	protected ANYImpl() {
		_isNull = false;
	}

	protected ANYImpl(boolean flag) {
		_isNull = flag;
		if (flag)
			_nullFlavor = CSImpl.nullCS(NullFlavorImpl.NI);
	}

	protected ANYImpl(boolean flag, CS cs) {
		_isNull = flag;
		_nullFlavor = cs;
	}

	public BN nonNull() {
		return BNImpl.valueOf(!_isNull);
	}

	public CS nullFlavor() {
		if (!_isNull)
			return CSImpl.nullCS(NullFlavorImpl.NI);
		else
			return _nullFlavor;
	}

	public BN isNull() {
		return BNImpl.valueOf(_isNull);
	}

	public BL notApplicable() {
		return NullFlavorImpl.notApplicable(_nullFlavor);
	}

	public BL unknown() {
		return NullFlavorImpl.unknown(_nullFlavor);
	}

	public BL other() {
		return NullFlavorImpl.other(_nullFlavor);
	}

//	public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//			throws Hl7Exception {
//		return null;
//	}

	public boolean equals(Object obj) {
		if (!(obj instanceof ANY))
			return false;
		else
			return equal((ANY) obj).isTrue();
	}

	protected static RuntimeException newRatioException(String s) {
		String as[] = { "CLASS" };
		String as1[] = { s };
		String s1 = MessageUtils.getMessage(
				"The denominator of a &CLASS may not be zero.", as, as1);
		return new IllegalArgumentException(s1);
	}

	protected static RuntimeException newTimezoneException(String s, String s1) {
		String as[] = { "LOW", "HIGH" };
		String as1[] = { s, s1 };
		String s2 = MessageUtils.getMessage(
						"Cannot compare TS \"&LOW\" with TS \"&HIGH\" because of incompatible timezone offsets (one of the timezones is missing).",
						as, as1);
		return new IllegalArgumentException(s2);
	}

	protected static RuntimeException newGTSSymbolException(String s) {
		String as[] = { "LITERAL" };
		String as1[] = { s };
		String s1 = MessageUtils.getMessage(
						"The symbolic abbreviation \"&LITERAL\" is not supported in this version of Healthcare.",
						as, as1);
		return new UnsupportedOperationException(s1);
	}

	protected static RuntimeException newHullIVLException(String s, String s1) {
		String as[] = { "LOW", "HIGH" };
		String as1[] = { s, s1 };
		String s2 = MessageUtils.getMessage(
						"Abbreviated hull form requires that the high timestamp literal \"&HIGH\" has the same as or fewer characters than the low timestamp \"&LOW\".",
						as, as1);
		return new IllegalArgumentException(s2);
	}

	protected static RuntimeException newTSPrecisionException(String s,
			String s1) {
		String as[] = { "CLASS", "PRECISION" };
		String as1[] = { s, s1 };
		String s2 = MessageUtils.getMessage(
						"&CLASS literal of length &PRECISION is not supported in this version of Healthcare.",
						as, as1);
		return new UnsupportedOperationException(s2);
	}

	protected static RuntimeException newDateException(String s) {
		String as[] = { "LITERAL" };
		String as1[] = { s };
		String s1 = MessageUtils.getMessage(
						"Date string \"&LITERAL\" is not recognized by SimpleDateFormat",
						as, as1);
		return new UnsupportedOperationException(s1);
	}

	protected static RuntimeException newIntervalException(String s, String s1,
			String s2) {
		String as[] = { "CLASS", "LOW", "HIGH" };
		String as1[] = { s, s1, s2 };
		String s3 = MessageUtils.getMessage(
						"Cannot construct interval with one boundary of \"&LOW\" and the other of \"&HIGH\". &CLASS must have matching boundary types.",
						as, as1);
		return new UnsupportedOperationException(s3);
	}

	protected static RuntimeException newUnsupportedException(String s,
			String s1) {
		String as[] = { "METHOD", "CLASS" };
		String as1[] = { s1, s };
		String s2 = MessageUtils.getMessage(
						"Operation &METHOD on data type &CLASS is not supported in this version of Healthcare.",
						as, as1);
		return new UnsupportedOperationException(s2);
	}

	public static Hl7Exception newParseException(String s, String s1,
			Throwable throwable) {
		String as[] = { "CLASS", "MESSAGE", "LITERAL" };
		String as1[] = { s, throwable.getMessage(), s1 };
		String s2 = MessageUtils.getMessage(
						"Invalid syntax encountered when parsing &CLASS literal: \"&LITERAL\"; &MESSAGE",
						as, as1);
		return new RimDataTypeException("CTB_CORE_GTS_PARSER", s2, throwable);
	}

	protected static RuntimeException newNullException(String s, String s1,
			String s2) {
		String as[] = { "PARAM", "METHOD", "CLASS" };
		String as1[] = { s2, s1, s };
		String s3 = MessageUtils.getMessage(
						"Mandatory parameter &PARAM must be supplied for operation &METHOD on data type &CLASS",
						as, as1);
		return new NullPointerException(s3);
	}

	protected static RuntimeException newBooleanException(String s) {
		String as[] = { "LITERAL" };
		String as1[] = { s };
		String s1 = MessageUtils.getMessage(
						"Boolean literal \"&LITERAL\" is not recognized. Must be either \"true\" or \"false\".",
						as, as1);
		return new IllegalArgumentException(s1);
	}

	public abstract TYPE dataType();

	public abstract BL equal(ANY any);
}
